package com.test.demo;

public class ArrayDemo5 {
    public static void main(String[] args) {
        //目标：认识二维数组
        printArray();
        printArray2();
    }

    //利用二维数组存储学生姓名
    public static void printArray() {
        //1.定义一个二维数组
        int[][] arr = new int[][]{ {12, 24, 36} , {666, 888} , {10, 20, 30} , {999} };
        //静态初始化二维数组
        String[][] classroom = {
                {"张三", "李四", "王五","测试"},//0 第一排
                {"赵六", "孙七", "周八"},//1 第二排
                {"郑九", "钱十", "吴十一"}//2 第三排
        };
        //动态初始化二维数组
        String[][] classroom2 = new String[3][4];

        //2.访问二维数组
        //访问1：数组名[行索引]
        String[] names = classroom[2];
        for (int i = 0; i < names.length; i++) {
            System.out.println(names[i]);//当成一维数组来遍历
        }
        //访问2：数组名[行索引][列索引]
        System.out.println(classroom[1][2]);//周八
        //长度访问3：数组名.length
        System.out.println(classroom.length);//行数 3
        System.out.println(classroom[0].length);//列数 4

    }

    //遍历二维数组
    public static void printArray2() {
        String[][] classroom = {
                {"张三", "李四", "王五"},//0 第一排
                {"赵六", "孙七", "周八"},//1 第二排
                {"郑九", "钱十", "吴十一"}//2 第三排
        };
        //第一种遍历方式
        for (int i = 0; i < classroom.length; i++) {
            String[] names = classroom[i];//获取每一排的学生姓名
            for (int j = 0; j < names.length; j++) {
                System.out.print(names[j] + " ");
            }
            System.out.println();
        }
        System.out.println("******************");
        //第二种遍历方式
        for (int i = 0; i < classroom.length; i++) {
            for (int j = 0; j < classroom[i].length; j++) {
                String data = classroom[i][j];
                System.out.print(data + " ");
            }
            System.out.println();
        }
    }
}

